var apiHost = wx.getStorageSync("apiHost"); //请求地址
var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

var AppidFlag = wx.getStorageSync('AppidFlag');

// 60秒倒计时
var countdown = 60;
var settime = function (that) {
  if (countdown == 0) {
    that.setData({
      is_show: true
    })
    countdown = 60;
    return;
  } else {
    that.setData({
      is_show: false,     //判断倒计时
      last_time: countdown
    })
    countdown--;
  }
  setTimeout(function () {
    settime(that)
  }, 1000)
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetails : '',      //订单详情
    cancelModel:false,      //取消订单模态框
    flag : '',              //返回标志位
    payModel: false,         //支付弹框
    payModelMethod: 'wx',   //默认支付方式
    balance : 0 ,          //账户余额
    isModal : false,
    is_show: true,        //是否展示
    cmModel :false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var orderNo = options.orderNo;

    if (options.flag){
      that.setData({
        flag: options.flag
      })
    }

    // orderNo = '201809031142228489';

    var token = wx.getStorageSync('token');         //用户的token信息
    var openid = wx.getStorageSync('openid');       //用户的openId
    var phone = wx.getStorageSync('phone');             //用户手机号

    that.setData({
      token: token,
      orderNo: orderNo,
      openid: openid,
      phone: phone
    })
    that.initOrdersDetail(orderNo, token);  //初始化订单详情
    that.getBalance(token);                 //获取用户余额
  },

  /**
   * 初始化订单详情
   */
  initOrdersDetail: function (orderNo, token){
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/orders/ordersDetail", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        orderNo: orderNo
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        console.log(res)
        if (code == "10000") {
          var orderDetails = res.data.data;
          that.setData({
            orderDetails: orderDetails
          });
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 去支付， 选择支付方式
   */
  goPay : function(){
    this.setData({
      payModel:true
    })
  },

  /**
   * 选择支付方式
   */
  changePay:function(e){
    var that = this;
    var paymethod = e.currentTarget.dataset.paymethod;
    that.setData({
      payModelMethod: paymethod
    })
  },

  /**
   * 获取用户余额
   */
  getBalance: function (token) {
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/users/balance", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        if (code == "10000") {
          //添加展示用户余额
          var balance = res.data.data.balance;
          that.setData({
            balance: balance
          });
        } else {
          that.showMsg(msg, 'none');
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 确定支付按钮
   */
  cancelPay:function(){
    var that = this;
    var payModelMethod = that.data.payModelMethod;   //支付方式
    var orderNo = that.data.orderNo;
    var token = that.data.token;

    if (payModelMethod == 'wx') {
      that.getWxPay(orderNo, token);
    } else {
      this.setData({
        isModal: true,
        payModel:false
      })
    }
  },

  /**
   * 调用微信支付
   */
  getWxPay: function (orderNo, token) {
    var that = this;
    var openid = that.data.openid;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/orders/xcx/pay", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        orderNo: orderNo,
        openId: openid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          that.wxPay(res.data.data, orderNo)
        } else {
          that.showMsg(msg, 'none');
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 调起微信支付
   */
  wxPay: function (respon, orderNo) {
    var that = this;
    //调支付接口
    wx.requestPayment({
      appId: respon.appId,
      timeStamp: respon.timeStamp,
      nonceStr: respon.nonceStr,
      'package': respon.package,
      signType: respon.signType,
      paySign: respon.paySign,
      'success': function (res) {
        that.reload();    //刷新页面
      },
      'fail': function (res) {
      }
    })
  },

  /**
   * 支付完成 刷新页面
   */
  reload:function(){
    var that = this;
    var token = that.data.token;
    var orderNo = that.data.orderNo;
    that.initOrdersDetail(orderNo, token);  //初始化订单详情
    that.cancel();
  },

  /**
   * 获取验证码
   */
  getCode: function () {
    var that = this;
    var token = that.data.token;

    // 将获取验证码按钮隐藏60s，60s后再次显示
    that.setData({
      is_show: (!that.data.is_show)  //false
    })
    settime(that);  //倒计时

    that.sendCode(token)//发送验证码
  },

  /**
   * 发送支付验证码
   */
  sendCode: function (token) {
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/login/payCode",
      data: {
        token: token,
        appId: AppidFlag,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          that.showMsg(msg, 'success');
        } else {
          that.showMsg(msg, 'none');
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 验证码输入框
   */
  getCodeValue: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },

  /**
   * 余额支付确认按钮
   */
  cancelbalance: function () {
    var that = this;
    var token = that.data.token;
    var orderNo = that.data.orderNo;
    var code = that.data.inputValue;

    if (code == '') {
      that.showMsg('请输入验证码', 'none');
    } else {
      that.banlancePay(token, orderNo, code);
    }
  },

  /**
   * 余额支付
   */
  banlancePay: function (token, orderNo, code) {
    var that = this;
    var openid = that.data.openid;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/banlance/pay", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        orderNo: orderNo,
        code: code
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          that.reload();    //刷新页面
        } else {
          that.showMsg(msg, 'none');
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 调起取消按钮弹窗
   */
  showcCancelorders : function(){
    var that = this;
    that.setData({
      cancelModel:true
    });
  },

  /**
   * 取消订单
   */
  cancelorders: function (){
    var that = this;
    var orderNo = that.data.orderNo;
    var token = that.data.token;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/orders/cancelorders", //仅为示例，并非真实的接口地址
      data: {
        orderNo: orderNo,
        token: token,
        appId: AppidFlag
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          that.initOrdersDetail(orderNo, token);  //初始化订单详情
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 删除订单
   */
  delOrder:function(){
    var that = this;
    that.setData({
      showModelDel: true
    });
  },
  /**
  * 删除订单按钮
  */
  confirmDel: function () {
    var that = this;
    var orderNo = that.data.orderNo;
    var token = that.data.token;

    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/orders/delorders", //仅为示例，并非真实的接口地址
      data: {
        orderNo: orderNo,
        token: token,
        appId: AppidFlag
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          that.showMsg(msg, 'success');
          that.closeModel();
          //刷新列表
          var state = that.data.state;
          if (state == 0) {
            that.initMyorders(token, 0, 10);
          } else {
            that.getOrdersByState(token, state, 0, 10);
          }

        } else {
          that.showMsg(msg, 'none');
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 确认收货
   */
  confirmReceive:function(e){
    var that = this;
    var skuId = e.currentTarget.dataset.skuid;
    that.setData({
      cmModel:true,
      skuId: skuId
    })
  },

  /**
   * 确认收货方法
   */
  cancelconfirmReceive: function (){
    var skuId = that.data.skuId;
    var token = that.data.token;
    var orderNo = that.data.orderNo;

    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/orders/confirmReceive", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        orderNo: orderNo,
        goodsId: skuId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          wx.showToast({
            title: "确认发货成功",
            icon: 'success',
            duration: 2000
          })
          that.reload();
        } else {
          wx.showToast({
            title: msg,
            icon: 'success',
            duration: 2000
          })
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 提醒发货
   */
  remindsend:function(e){
    var that = this;
    var skuId = e.currentTarget.dataset.skuid;
    var token = that.data.token;
    var orderNo = that.data.orderNo;

    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/orders/remindsend", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        orderNo: orderNo,
        skuId: skuId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          wx.showToast({
            title: "提醒发货成功",
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'success',
            duration: 2000
          })
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 复制剪切板
   */
  copyTBL: function (e) {
    var that = this;
    wx.setClipboardData({
      data: that.data.orderDetails.orderNo,
      success: function (res) {
        wx.showToast({
          title: "复制成功",
          icon: 'success',
          duration: 2000
        })
      }
    });
  },

  /**
   * 跳转查看物流
   */
  goLogistics:function(e){
    var that = this;
    var skuId = e.currentTarget.dataset.skuid;
    var orderNo = that.data.orderNo;
    that.toPage("../logistics/logistics?orderNo=" + orderNo + "&skuId=" + skuId)
  },

  goFillOrder : function(){
    var that = this;
    that.toPage("../order/fillOrder/fillOrder?skuidcounts=" + skuIdCounts)

  },

  /**
   * 弹窗取消按钮 
   */
  cancel: function () {
    this.setData({
      cancelModel: false,
      payModel : false,
      isModal:false,
      cmModel:false,
      showModelDel:false
    })
  },

  //跳转网页
  toPage: function (url) {
    // 加载中
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.navigateTo({
      url: url,
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    var flag = that.data.flag
    if (flag==''){
      wx.reLaunch({
        url: "../../shopCart/shopCart",
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  /**
 * 提示信息
 */
  showMsg: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      mask: true
    });
  },
})